package com.example.arithmeticleetcode.leetcode.october;

import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-10-26 09:11
 **/
public class Demo15 {

    public static void main(String[] args) {

    }

    public static char slowestKey(int[] releaseTimes, String keysPressed) {
        Map<Integer, List<Character>> map = new HashMap<>();
        List<Character> firstList = new ArrayList<>();
        firstList.add(keysPressed.charAt(0));
        map.put(releaseTimes[0], firstList);
        for (int i = 1, len = keysPressed.length(); i < len; i++) {
            int timeDistance = releaseTimes[i] - releaseTimes[i - 1];
            if (map.containsKey(timeDistance)) {
                List<Character> characters = map.get(timeDistance);
                characters.add(keysPressed.charAt(i));
                map.put(timeDistance, characters);
            } else {
                List<Character> characters =  new ArrayList<>();
                characters.add(keysPressed.charAt(i));
                map.put(timeDistance, characters);
            }
        }
        int maxValue = map.keySet().stream().max((e1, e2) -> e1.compareTo(e2)).get();
        List<Character> characters = map.get(maxValue);
        Collections.sort(characters);
        return characters.get(characters.size() - 1);
    }

    public static char slowestKey2(int[] releaseTimes, String keysPressed) {
        int max = Integer.MIN_VALUE;
        char result = keysPressed.charAt(0);
        for (int i = 1, len = keysPressed.length(); i < len; i++) {
            int timeDistance = releaseTimes[i] - releaseTimes[i - 1];
            if (timeDistance > max) {
                max = timeDistance;
                result = keysPressed.charAt(i);

            }
        }
        return max < releaseTimes[0] ? keysPressed.charAt(0) : result;
    }
}
